from infirmary_admin_src.infirmary_common.infirmary_controller import IBaseController
from infirmary_admin_src.infirmary_common.infirmary_controller.rest_controller_with_depends import \
    RestControllerWithDepends
from fastapi import Request, Depends, Path, Body

from infirmary_admin_src.infirmary_smart_admin.infirmary_biz.infirmary.businesses.schemas import SchemaBase
from infirmary_admin_src.infirmary_smart_admin.infirmary_biz.infirmary.businesses.services.service_businesses import \
    Businesses



class XParam(SchemaBase):
    '''
    账号密码登录参数
    '''
    pass
    appid:str= ''
    name: str = ''
    tel: str = ''
    status: int = 0
    pageNum: int = 1
    pageSize: int = 10


class IController(IBaseController):
    '''
    定制相关约束检测函数逻辑步序
    '''

    def __init__(self, *, request: Request,
                 schema_param: XParam = Depends(),
                 ):
        super().__init__(request=request)
        # 入参参数
        # 入参参数
        self.schema_param = schema_param


@RestControllerWithDepends()
class InfirmaryBusinessesDoctorQuestListController(IController):

    def business_login(self):
        pass

        # 限制渠道

        if self.request.state.department_code not in ['infirmary_group', 'infirmary_group_in']:
            # 限制查询的数据
            if hasattr(self.schema_param, 'appid'):
                self.schema_param.appid = self.request.state.department_code

        _result_nums_total, lists = Businesses.get_businesses_doctor_list(
            appid=self.schema_param.appid,
            tel = self.schema_param.tel,
            name=self.schema_param.name, status=self.schema_param.status, pageNo=self.schema_param.pageNum,
            pageSize=self.schema_param.pageSize, is_del_select_key=[], del_flag=0,
        )

        def addurlimge(v):
            # http://infirmary.wohuayuan.com/?appid=GZYYT&code=011nroFa1ugNqF0g8kJa1LRBSi4nroF1&state=STATE
            # ''.startswith()
            v['icon'] = v.get("icon") if v.get("icon") and v.get("icon").startswith(
                'http') else ('暂无医生图像' if not v.get("icon") else f'http://infirmary.wohuayuan.com{v.get("icon")}')

            # 需要转换一下相关01 属性
            v['is_admin'] =True  if v.get("is_admin")==1 else False
            v['is_rest'] = True if v.get("is_rest") == 1 else False
            v['status'] = True if v.get("status") == 1 else False
            v['is_notice_to_doctor'] = True if v.get("is_notice_to_doctor") == 1 else False

            #   station_name = TextField(constraints=[SQL("DEFAULT ''::text")], null=True, verbose_name="医生驻点主场医院")
            #     is_more_station = IntegerField(constraints=[SQL("DEFAULT 0")], null=True, verbose_name="是否多点执业 0 否 1 是")
            #     is_rest = IntegerField(constraints=[SQL("DEFAULT 0")], null=True, verbose_name="是否休息 0 否 1 是")
            #     status = IntegerField(constraints=[SQL("DEFAULT 0")], null=True, verbose_name="是否禁用 0 否 1 是")
            #     sort = IntegerField(constraints=[SQL("DEFAULT 1")], null=True, verbose_name="排序")
            #     excel = TextField(constraints=[SQL("DEFAULT ''::text")], null=True, verbose_name="擅长领域说明")
            #     ctime = DateTimeField(constraints=[SQL("DEFAULT now()")], null=True, verbose_name="创建时间")
            #
            #     is_admin = IntegerField(constraints=[SQL("DEFAULT 0")], null=True, verbose_name="是否是管理员")
            #     is_notice_to_doctor = IntegerField(constraints=[SQL("DEFAULT 0")], null=True, verbose_name="预约后是否通知医生")
            #     refundpassword = TextField(constraints=[SQL("DEFAULT ''::text")], null=True, verbose_name="退款操作密码设置")
            #     openid = TextField(constraints=[SQL("DEFAULT ''::text")], null=True, verbose_name="医生对应公众号的openid")


            return v
        return {
            'emptyFlag': False if _result_nums_total > 0 else True,
            'list': [addurlimge(v) for v in lists],
            'pageNum': self.schema_param.pageNum,
            'pageSize': self.schema_param.pageSize,
            'pages': _result_nums_total / len(lists) if _result_nums_total > 0 else 0,
            'total': _result_nums_total
        }
